package org.gcb.core.model;

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.IOException;
import javax.swing.Icon;

/**
 *
 * @author Jason
 */
public class Item implements Transferable{
    public static final DataFlavor ITEM_DATA_FLAVOR = new DataFlavor(Item.class, "item");
    public static final String PROP_TEXT = "text";
    public static final String PROP_DESCRIPTION = "description";
    public static final String PROP_WEIGHT = "weight";
    public static final String PROP_PRICE = "price";
    public static final String PROP_ICON = "icon";
    protected final Icon icon;
    protected final String text;
    protected String description;
    protected Double weight = 1.0;
    protected Object price;
    private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);

    public Item(Icon icon, String text) {
	this.icon = icon;
	this.text = text;
    }

    public Item(Icon icon, String text, Double weight) {
	this.icon = icon;
	this.text = text;
	this.weight = weight;
    }

    public Item(Icon icon, String text, String description) {
	this.icon = icon;
	this.text = text;
	this.description = description;
    }

    public Item(Icon icon, String text, String description, Object price) {
	this.icon = icon;
	this.text = text;
	this.description = description;
	this.price = price;
    }

    public Item(Icon icon, String text, String description, Double weight, Object price) {
	this.icon = icon;
	this.text = text;
	this.description = description;
	this.weight = weight;
	this.price = price;
    }

    /**
     * Get the value of icon
     *
     * @return the value of icon
     */
    public Icon getIcon() {
	return icon;
    }

    /**
     * Get the value of price
     *
     * @return the value of price
     */
    public Object getPrice() {
	return price;
    }

    /**
     * Set the value of price
     *
     * @param price new value of price
     */
    public void setPrice(Object price) {
	Object oldPrice = this.price;
	this.price = price;
	propertyChangeSupport.firePropertyChange(PROP_PRICE, oldPrice, price);
    }


    /**
     * Get the value of weight
     *
     * @return the value of weight
     */
    public Double getWeight() {
	return weight;
    }

    /**
     * Set the value of weight
     *
     * @param weight new value of weight
     */
    public void setWeight(Double weight) {
	Double oldWeight = this.weight;
	this.weight = weight;
	propertyChangeSupport.firePropertyChange(PROP_WEIGHT, oldWeight, weight);
    }


    /**
     * Get the value of description
     *
     * @return the value of description
     */
    public String getDescription() {
	return description;
    }

    /**
     * Set the value of description
     *
     * @param description new value of description
     */
    public void setDescription(String description) {
	String oldDescription = this.description;
	this.description = description;
	propertyChangeSupport.firePropertyChange(PROP_DESCRIPTION, oldDescription, description);
    }


    /**
     * Get the value of text
     *
     * @return the value of text
     */
    public String getText() {
	return text;
    }

    /**
     * Add PropertyChangeListener.
     *
     * @param listener
     */
    public void addPropertyChangeListener(PropertyChangeListener listener) {
	propertyChangeSupport.addPropertyChangeListener(listener);
    }

    /**
     * Remove PropertyChangeListener.
     *
     * @param listener
     */
    public void removePropertyChangeListener(PropertyChangeListener listener) {
	propertyChangeSupport.removePropertyChangeListener(listener);
    }

    @Override
    public DataFlavor[] getTransferDataFlavors() {
	return new DataFlavor[]{ITEM_DATA_FLAVOR};
    }

    @Override
    public boolean isDataFlavorSupported(DataFlavor flavor) {
	return flavor==ITEM_DATA_FLAVOR;
    }

    @Override
    public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
	if (flavor==ITEM_DATA_FLAVOR) {
	    return this;
	}
	throw new UnsupportedFlavorException(flavor);
    }
}
